--
-- Author: mm
-- Date: 2018-05-07 18:27:16
--
-- GameplaypreviewSmlRender
--

local string_split = string.split

local Skill = require("app.public.dao.Skill")
local Equip = require("app.public.dao.Equip")

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.GamePlayPreviewNode_3))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )

    display.uiAddClick( self._ui.Panel, function()
        local contentType = self._data.content
        if 1 == contentType then -- 技能
            if self._skillTemp then
                local boundingBox = self._ui.bg:getBoundingBox()
                local worldPos = self._ui.bg:convertToWorldSpace(cc.p(0, 0))
                boundingBox.x = worldPos.x
                boundingBox.y = worldPos.y
                local params = {
                    tipsType = Enums.TipsType.skill,
                    dataTemp = self._skillTemp,
                    boundingBox = boundingBox
                }
                app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params=params})
            end
        elseif 2 == contentType then -- 装备
            if self._equipTemp then
                local boundingBox = self._ui.bg:getBoundingBox()
                local worldPos = self._ui.bg:convertToWorldSpace(cc.p(0, 0))
                boundingBox.x = worldPos.x
                boundingBox.y = worldPos.y
                local params = {
                    tipsType = Enums.TipsType.equip,
                    dataTemp = self._equipTemp,
                    boundingBox = boundingBox
                }
                app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params=params})
            end
        end
    end)

    self:refresh()
end

-- 自动适配文本高度
local function _autoJustTextHeight( noteNode )
    local size = noteNode:getContentSize()
    noteNode:getVirtualRenderer():setDimensions( size.width, 0 )
    local afterSize = noteNode:getVirtualRendererSize()
    noteNode:setContentSize( cc.size( size.width, afterSize.height ) )
    ccui.Helper:doLayout( noteNode )
end

function ClassRef:refresh()
    if self._data then
        self:setVisible( true )
        local data = self._data

        local bgPath = ""
        if string.find( data.funcBg, ";" ) then
            local bgList = string_split( data.funcBg, ";" )
            for i, v in ipairs( bgList ) do
                local tempList = string_split( v, "|" )
                if UD:getClass() == tonumber( tempList[1] ) then -- 自己职业？
                    bgPath = GD:getImagePath( Res.gamePlayPath, tempList[2] )
                    break
                end
            end
        else
            bgPath = GD:getImagePath( Res.gamePlayPath, data.funcBg )
        end
        self._ui.img:loadTexture( bgPath )
        self._ui.bg:loadTexture( bgPath )

        local sloganStr = ""
        if string.find( data.slogan, ";" ) then -- 需要根据职业解析文本
            local sloganList = string_split( data.slogan, ";" )
            for i, v in ipairs( sloganList ) do
                local tempList = string_split( v, "|" )
                if UD:getClass() == tonumber( tempList[1] ) then -- 自己职业？
                    sloganStr = L( tempList[2] )
                    break
                end
            end
        else
            sloganStr = L( data.slogan )
        end
        self._ui.text:setString( sloganStr )
        _autoJustTextHeight( self._ui.text )

        -- 1=技能
        -- 2=装备
        -- 3=地图
        -- 4=功能
        -- 5=副本
        -- 6=专精
        local contentType = data.content
        if 1 == contentType then -- 技能？
            self._ui.bg:setVisible( true )
            self._ui.img:setVisible( false )
            self._ui.skill_icon:setVisible( true )
            self._ui.arms_icon:setVisible( false )

            -- 技能数据
            local skillId = 0
            local valueList = string_split( data.value, ";" )
            for i, v in ipairs( valueList ) do
                local tempList = string_split( v, "|" )
                if UD:getClass() == tonumber( tempList[1] ) then -- 自己职业？
                    skillId = tonumber( tempList[2] )
                    break
                end
            end
            self._skillTemp = Skill.new()
            self._skillTemp:read( { uid = skillId } )

            self._ui.tlt:setString( string.format( L( data.title ), self._skillTemp:getName() ) )
            self._ui.skill_icon:loadTexture( self._skillTemp:getIcon() )
        elseif 2 == contentType then -- 装备？
            self._ui.bg:setVisible( true )
            self._ui.img:setVisible( false )
            self._ui.skill_icon:setVisible( false )
            self._ui.arms_icon:setVisible( true )

            -- 装备数据
            local equipId = UD:parseClassText( data.value )
            self._equipTemp = Equip.new( true )
            self._equipTemp:read( { tid = tonumber( equipId ) } )

            self._ui.tlt:setString( string.format( L( data.title ), self._equipTemp:getName() ) )
            self._ui.arms_icon:loadTexture( GD:getImagePath( Res.gamePlayPath, equipId ) )
        elseif 6 == contentType then -- 专精
            self._ui.bg:setVisible( false )
            self._ui.img:setVisible( true )
            self._ui.skill_icon:setVisible( false )
            self._ui.arms_icon:setVisible( false )

            local titleStr = ""
            if string.find( data.title, ";" ) then -- 需要根据职业解析文本
                local titleList = string_split( data.title, ";" )
                for i, v in ipairs( titleList ) do
                    local tempList = string_split( v, "|" )
                    if UD:getClass() == tonumber( tempList[1] ) then -- 自己职业？
                        titleStr = L( tempList[2] )
                        break
                    end
                end
            else
                titleStr = L( data.title )
            end
            self._ui.tlt:setString( titleStr )
        else -- 其他？
            self._ui.bg:setVisible( false )
            self._ui.img:setVisible( true )
            self._ui.skill_icon:setVisible( false )
            self._ui.arms_icon:setVisible( false )

            self._ui.tlt:setString( L( data.title ) )
        end

        local unlockList = string_split( data.unlock, "|" )
        local typeTemp = tonumber( unlockList[1] )
        if 1 == typeTemp then -- 等级？
            if tonumber( unlockList[2] ) > UD:getLevel() then -- 等级不足，未解锁？
                self._ui.tips:setString( L( data.tips ) )
            else
                self._ui.tips:setString( L( data.finish ) )
            end
        elseif 2 == typeTemp then -- 任务？
            if UD:isMainTaskFinished( unlockList[2] ) then -- 任务完成？
                self._ui.tips:setString( L( data.finish ) )
            else
                self._ui.tips:setString( L( data.tips ) )
            end
        end
    else
        self:setVisible( false )
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
